package com.javaclimb.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.javaclimb.mappers.CommentMapper;
import com.javaclimb.pojo.Comment;
import com.javaclimb.pojo.User;
import com.javaclimb.service.CommentService;
import com.javaclimb.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImp implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public boolean addComment(Comment comment) {
        return commentMapper.insetComment(comment)>0;
    }

    @Override
    public List<Comment> getComments(Integer id) {
        List<Comment> comments = commentMapper.selectCommentByTreeId(id);
        return comments;
    }

    @Override
    public boolean addLike(Integer id) {
        return commentMapper.addLike(id)>0;
    }

    @Override
    public boolean deleteComment(Integer id) {
        return commentMapper.deleteComment(id)>0;
    }

    @Override
    public Comment checkIsComment(Integer userId, Integer treeId) {
        return commentMapper.checkIsComment(userId,treeId);
    }

    @Override
    public List<Comment> getCommentsById(Integer id) {
        return commentMapper.list(id);
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, Integer id, Integer userId, Integer treeId, Date startTime, Date endTime) {
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
        //2.执行查询
        List<Comment> commentList = commentMapper.listAll(id,userId,treeId,startTime,endTime);//传递条件查询参数
        Page<Comment> p = (Page<Comment>) commentList;
        //3.封装bean对象
        PageBean pB = new PageBean(p.getTotal(), p.getResult());
        return pB;
    }

    @Override
    public void delete(List<Integer> ids) {
        commentMapper.deleteComments(ids);
    }
}
